//
//  WHScanWithImgView.m
//  WHEasyBizLink
//
//  Created by hyf on 2024/11/28.
//

#import "WHScanWithImgView.h"
#import "UIView+Rounded.h"
#import "SDAutoLayout.h"

@interface WHScanWithImgView ()
@property (nonatomic, strong) UIImageView *mImgPreview;
@property (nonatomic, strong) UIButton *mRedSpotButton;
//图片数量
@property (nonatomic,assign) int mImgNum;
@end

@implementation WHScanWithImgView

-(void)setupScanningQRCodeEdging{
    [super setupScanningQRCodeEdging];
    //照片缩略图
    UIImageView *imgPreview = [[UIImageView alloc] init];
    imgPreview.backgroundColor = [UIColor blackColor];
    CGFloat imgPrev_buttonX = 30;
    CGFloat imgPrev_buttonY = CGRectGetMaxY(self.light_button.frame) + scanContent_X * 0.5 + 10;
    CGFloat imgPrev_buttonW = 70;
    CGFloat imgPrev_buttonH = 70;
    imgPreview.frame = CGRectMake(imgPrev_buttonX, imgPrev_buttonY, imgPrev_buttonW, imgPrev_buttonH);
    self.mImgPreview = imgPreview;
    [UIView setViewRoundedWithRad:10 byRoundingCorners:UIRectCornerAllCorners view:self.mImgPreview];
    
    imgPreview.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector (imgPreviewTapGesture)];
    tapGesture.numberOfTapsRequired    = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [imgPreview addGestureRecognizer:tapGesture];
    [self.bottom_View addSubview:imgPreview];
    
    //红点按钮
    [self.bottom_View addSubview:self.mRedSpotButton];
    self.mRedSpotButton
        .sd_layout
        .rightSpaceToView(self.mImgPreview, -83)
        .topSpaceToView(self.mImgPreview, -83)
        .widthIs(26)
        .heightIs(26);
}

#pragma mark 设置最新拍照图片
-(void) setMImgPreviewImg:(UIImage *)img num:(int)num{
    self.mImgPreview.image = img;
    self.mImgNum = num;
    self.mRedSpotButton.hidden = NO;
    [self.mRedSpotButton setTitle:[NSString stringWithFormat:@"%d",self.mImgNum] forState:UIControlStateNormal];
    if(self.mImgNum>99){
        [self.mRedSpotButton setTitle:[NSString stringWithFormat:@"%@",@"99+"] forState:UIControlStateNormal];
    }
}

#pragma mark 缩略图点击
-(void)imgPreviewTapGesture{
    if(self.imgPreviewBlock)
    {
        self.imgPreviewBlock();
    }
}

-(UIButton *)mRedSpotButton
{
    if (!_mRedSpotButton) {
        
        _mRedSpotButton = [[UIButton alloc] init];
        [_mRedSpotButton setTitle:@"0" forState:UIControlStateNormal];
        [_mRedSpotButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _mRedSpotButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [UIView setViewRoundedWithRad:50 byRoundingCorners:UIRectCornerAllCorners view:_mRedSpotButton];
        _mRedSpotButton.backgroundColor = [UIColor redColor];
        _mRedSpotButton.hidden = YES;
    }
    return _mRedSpotButton;
}
@end
